package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.constant.WxOperateType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysWxGroup;
import com.ruoyi.system.domain.SysWxOperateRecord;
import com.ruoyi.system.mapper.SysWxGroupMapper;
import com.ruoyi.system.mapper.SysWxOperateRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysWxapplyRecordMapper;
import com.ruoyi.system.domain.SysWxapplyRecord;
import com.ruoyi.system.service.ISysWxapplyRecordService;

/**
 * 加群申请记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-28
 */
@Service
public class SysWxapplyRecordServiceImpl implements ISysWxapplyRecordService 
{
    @Autowired
    private SysWxapplyRecordMapper sysWxapplyRecordMapper;
    @Autowired
    private SysWxGroupMapper sysWxGroupMapper;

    @Autowired
    private SysWxOperateRecordMapper sysWxOperateRecordMapper;

    /**
     * 查询加群申请记录
     * 
     * @param id 加群申请记录主键
     * @return 加群申请记录
     */
    @Override
    public SysWxapplyRecord selectSysWxapplyRecordById(Long id)
    {
        return sysWxapplyRecordMapper.selectSysWxapplyRecordById(id);
    }

    /**
     * 查询加群申请记录列表
     * 
     * @param sysWxapplyRecord 加群申请记录
     * @return 加群申请记录
     */
    @Override
    public List<SysWxapplyRecord> selectSysWxapplyRecordList(SysWxapplyRecord sysWxapplyRecord)
    {
        return sysWxapplyRecordMapper.selectSysWxapplyRecordList(sysWxapplyRecord);
    }

    /**
     * 新增加群申请记录
     * 
     * @param sysWxapplyRecord 加群申请记录
     * @return 结果
     */
    @Override
    public int insertSysWxapplyRecord(SysWxapplyRecord sysWxapplyRecord)
    {
        sysWxapplyRecord.setCreateTime(DateUtils.getNowDate());
        return sysWxapplyRecordMapper.insertSysWxapplyRecord(sysWxapplyRecord);
    }

    /**
     * 修改加群申请记录
     * 
     * @param sysWxapplyRecord 加群申请记录
     * @return 结果
     */
    @Override
    public int updateSysWxapplyRecord(SysWxapplyRecord sysWxapplyRecord) throws Exception
    {
        sysWxapplyRecord.setUpdateTime(DateUtils.getNowDate());
        //将该条记录标识改为 申请中
        if (sysWxapplyRecord.getStatus()!=null&&sysWxapplyRecord.getStatus().trim().equals("0")){
            //表示加群成功，此时将成员添加进来
            if (sysWxapplyRecord.getMember()!=null&&(!sysWxapplyRecord.getMember().trim().isEmpty())){

                List<String> needInserList=new ArrayList<>();
                List<String> allMemberList=new ArrayList<>();
                    SysWxGroup inner=    sysWxGroupMapper.selectSysWxGroupById(sysWxapplyRecord.getGroupid());
                    //之前已经有成员
                    if (inner.getMember()!=null&&!(inner.getMember().trim().isEmpty())){
                        String[] innerMember=inner.getMember().trim().split(",");
                        for (int i=0;i<innerMember.length;i++){
                            allMemberList.add(innerMember[i]);
                        }
                            boolean isinsert=true;
                            for (int j=0;j<innerMember.length;j++){
                                if (innerMember[j].trim().equals(sysWxapplyRecord.getMember().trim())){
                                    isinsert=false;
                                }
                            }
                            if (isinsert){
                                needInserList.add(sysWxapplyRecord.getMember());
                            }
                        allMemberList.addAll(needInserList);

                    }else {
                            allMemberList.add(sysWxapplyRecord.getMember());

                    }
                    //toto  将新增记录  插入表中
                    if (needInserList!=null&&needInserList.size()>0){
                        SysWxOperateRecord sysWxOperateRecord=new SysWxOperateRecord();
                        sysWxOperateRecord.setWxgroupid(inner.getId());
                        sysWxOperateRecord.setWxOpareteType(WxOperateType.ADD_PERSON);
                        sysWxOperateRecord.setGroupNumber(inner.getGroupNumber());
                        sysWxOperateRecord.setGroupownerPhone(inner.getGroupownerPhone());
                        sysWxOperateRecord.setGroupownerName(inner.getGroupownerName());
                        sysWxOperateRecord.setRemark("添加成员,成员id为--"+String.join(",",needInserList));
                        sysWxOperateRecordMapper.insertSysWxOperateRecord(sysWxOperateRecord);
                        //更新加群记录编号
                        sysWxapplyRecord.setGroupNumber(inner.getGroupNumber());
                        sysWxapplyRecordMapper.updateSysWxapplyRecord(sysWxapplyRecord);
                    }
                //将该条群  加人申请中状态改为  可申请
                 inner.setAttribute3("可申请");
                inner.setAttribute1("0");
                inner.setAttribute5(DateUtils.getNowDate());
                inner.setMember(String.join(",",allMemberList));
                sysWxGroupMapper.updateSysWxGroup(inner);

            }
        }//如果失效 改为失效
        else   if (sysWxapplyRecord.getStatus()!=null&&sysWxapplyRecord.getStatus().trim().equals("-1")){
            SysWxGroup inner=    sysWxGroupMapper.selectSysWxGroupById(sysWxapplyRecord.getGroupid());
            //判断是否为初次申请，如果是初次申请，则删除掉
            if (inner!=null&&(inner.getAttribute1()!=null&&inner.getAttribute1().trim().equals("0"))){
                inner.setAttribute3("存在请求失效");
                inner.setAttribute5(DateUtils.getNowDate());
                sysWxGroupMapper.updateSysWxGroup(inner);

            }else {
                if (inner!=null&&inner.getId()!=null){
                sysWxGroupMapper.deleteSysWxGroupById(inner.getId());
                }
            }

        }


        return sysWxapplyRecordMapper.updateSysWxapplyRecord(sysWxapplyRecord);
    }

    /**
     * 批量删除加群申请记录
     * 
     * @param ids 需要删除的加群申请记录主键
     * @return 结果
     */
    @Override
    public int deleteSysWxapplyRecordByIds(Long[] ids)
    {
        return sysWxapplyRecordMapper.deleteSysWxapplyRecordByIds(ids);
    }

    /**
     * 删除加群申请记录信息
     * 
     * @param id 加群申请记录主键
     * @return 结果
     */
    @Override
    public int deleteSysWxapplyRecordById(Long id)
    {
        return sysWxapplyRecordMapper.deleteSysWxapplyRecordById(id);
    }
}
